﻿namespace WinRTClient.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Threading.Tasks;
    using System.Windows.Input;

    using dot_in.HTWKAidStation.Client;
    using dot_in.HTWKAidStation.Client.Mensa;
    using dot_in.HTWKAidStation.Client.Menu;

    using StyleMVVM.ViewModel;

    public class MenuWeekViewModel : PageViewModel
    {
        private bool isBusy;
        private string pageTitle;

        public MenuWeekViewModel()
        {
            this.MenuWeekItems = new ObservableCollection<MenuWeekGroup>();
        }

        public ICommand BackCommand
        {
            get
            {
                return new DelegateCommand((x) => Navigation.GoBack());
            }
        }

        public bool IsBusy
        {
            get
            {
                return this.isBusy;
            }

            set
            {
                this.isBusy = value;
                this.OnPropertyChanged();
            }
        }

        public ObservableCollection<MenuWeekGroup> MenuWeekItems { get; set; }

        public string PageTitle
        {
            get
            {
                return this.pageTitle;
            }

            set
            {
                this.pageTitle = value;
                this.OnPropertyChanged();
            }
        }

        protected override void OnNavigatedTo(object sender, StyleMVVM.View.StyleNavigationEventArgs e)
        {
            this.PageTitle = "Speiseplan der " + this.NavigationParameter;
            this.LoadData(((Mensa)this.NavigationParameter).ID);
            base.OnNavigatedTo(sender, e);
        }

        private async void LoadData(int mensaID)
        {
            this.IsBusy = true;
            List<MenuWeekGroup> menuWeekGroups = await Core.ParseMenu(mensaID, 7);
            foreach (MenuWeekGroup menuWeekGroup in menuWeekGroups)
            {
                this.MenuWeekItems.Add(menuWeekGroup);
            }

            this.IsBusy = false;
        }
    }
}
